﻿using helper.Commons.KnownBoxes;
using System;
using System.Collections;
using System.Collections.Generic;

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace ZuesControls.Controls
{
    public class ZSDataGridExpander:Expander
    {
        public static readonly DependencyProperty ColumnHeaderContainerStyleProperty =
            DependencyProperty.Register("ColumnHeaderContainerStyle", typeof(Style), typeof(ZSDataGridExpander));

        public static readonly DependencyProperty ColumnHeaderTemplateSelectorProperty =
            DependencyProperty.Register("ColumnHeaderTemplateSelector", typeof(DataTemplateSelector), typeof(ZSDataGridExpander), new FrameworkPropertyMetadata());

        public static readonly DependencyProperty ColumnHeaderStringFormatProperty =
            DependencyProperty.Register("ColumnHeaderStringFormat", typeof(string), typeof(ZSDataGridExpander));

        public static readonly DependencyProperty AllowsColumnReorderProperty =
            DependencyProperty.Register("AllowsColumnReorder", typeof(bool), typeof(ZSDataGridExpander), new FrameworkPropertyMetadata(BooleanBoxes.TrueBox));

        public static readonly DependencyProperty ColumnHeaderContextMenuProperty =
            DependencyProperty.Register("ColumnHeaderContextMenu", typeof(ContextMenu), typeof(ZSDataGridExpander));

        public static readonly DependencyProperty ColumnHeaderToolTipProperty =
            DependencyProperty.Register("ColumnHeaderToolTip", typeof(object), typeof(ZSDataGridExpander));

        /// <summary>
        /// 列集合
        /// </summary>
        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register("Columns", typeof(GridViewColumnCollection), typeof(ZSDataGridExpander), new PropertyMetadata(null));

        /// <summary>
        /// 网格线可见性
        /// </summary>
        public static readonly DependencyProperty GridLinesVisibilityProperty =
            DependencyProperty.Register("GridLinesVisibility", typeof(DataGridGridLinesVisibility), typeof(ZSDataGridExpander), new PropertyMetadata(DataGridGridLinesVisibility.None));

        public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ZSDataGridExpander));

      

        public static readonly DependencyProperty ItemTemplateProperty =
            DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ZSDataGridExpander));

      

        
        static ZSDataGridExpander()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZSDataGridExpander), new FrameworkPropertyMetadata(typeof(ZSDataGridExpander)));
        }

        public Style ColumnHeaderContainerStyle
        {
            get => (Style)GetValue(ColumnHeaderContainerStyleProperty);
            set => SetValue(ColumnHeaderContainerStyleProperty, value);
        }

        public DataTemplateSelector ColumnHeaderTemplateSelector
        {
            get => (DataTemplateSelector)GetValue(ColumnHeaderTemplateSelectorProperty);
            set => SetValue(ColumnHeaderTemplateSelectorProperty, value);
        }

        public string ColumnHeaderStringFormat
        {
            get => (string)GetValue(ColumnHeaderStringFormatProperty);
            set => SetValue(ColumnHeaderStringFormatProperty, value);
        }
        public IEnumerable ItemsSource
        {
            get => (IEnumerable)GetValue(ItemsSourceProperty);
            set => SetValue(ItemsSourceProperty, value);
        }
        public bool AllowsColumnReorder
        {
            get => (bool)GetValue(AllowsColumnReorderProperty);
            set => SetValue(AllowsColumnReorderProperty, BooleanBoxes.Box(value));
        }

        public ContextMenu ColumnHeaderContextMenu
        {
            get => (ContextMenu)GetValue(ColumnHeaderContextMenuProperty);
            set => SetValue(ColumnHeaderContextMenuProperty, value);
        }

        public DataTemplate ItemTemplate
        {
            get => (DataTemplate)GetValue(ItemTemplateProperty);
            set => SetValue(ItemTemplateProperty, value);
        }

        public object ColumnHeaderToolTip
        {
            get => GetValue(ColumnHeaderToolTipProperty);
            set => SetValue(ColumnHeaderToolTipProperty, value);
        }

        /// <summary>
        /// 列集合
        /// </summary>
        public GridViewColumnCollection Columns
        {
            get { return (GridViewColumnCollection)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }

        /// <summary>
        ///  网格线可见性
        /// </summary>
        public DataGridGridLinesVisibility GridLinesVisibility
        {
            get { return (DataGridGridLinesVisibility)GetValue(GridLinesVisibilityProperty); }
            set { SetValue(GridLinesVisibilityProperty, value); }
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var dataGrid = GetTemplateChild("PART_DataGrid") as DataGrid;
            if (dataGrid != null && Columns != null)
            {
                dataGrid.Columns.Clear();
                foreach (var col in Columns)
                {
                    // 你需要将 GridViewColumn 映射成 DataGridColumn
                    var dgCol = ConvertGridViewColumnToDataGridColumn(col);
                    if (dgCol != null)
                        dataGrid.Columns.Add(dgCol);
                }
            }
        }

        private DataGridColumn ConvertGridViewColumnToDataGridColumn(GridViewColumn gvColumn)
        {
            // 这里只处理 DisplayMemberBinding 的转换
            if (gvColumn.DisplayMemberBinding is Binding binding)
            {
                return new DataGridTextColumn
                {
                    Header = gvColumn.Header,
                    Binding = binding,
                    Foreground = Brushes.Black,
                    Width = 80
                };
            }

            return null;
        }

        public ZSDataGridExpander()
        {
            Columns = new GridViewColumnCollection();
        }
    }
}
